package com.quarantine.web.admin;

//import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
//import java.net.URLEncoder;
import java.util.List;

//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
//import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.quarantine.model.fileupload;
import com.quarantine.service.FileUploadManager;
import com.quarantine.util.DateUtils;
import com.quarantine.web.CrudActionSupport;
@Namespace("/admin")
@Results({
	@Result(name ="save", location = "file-upload-admin!list.action", type ="redirect",params={"currentpage","${currentpage}"}),
	@Result(name ="delete", location = "file-upload-admin!list.action", type ="redirect",params={"currentpage","${currentpage}"}),
})
public class FileUploadAdminAction extends CrudActionSupport<fileupload> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private fileupload entity;
	private FileUploadManager fileupload;
	private File pictruepath;
	private String pictruepathFileName;
	private String pictruepathContenttype;
	private String id;
	private String ids;// 逗号隔开id集合
	private List<fileupload> fileList;
	private String name;
	private String editTime;
	/**
	 * 分頁用
	 */
	private int currentpage;
	private int pageNo;
	public static final int pageSize = 20;
	private int prePage;
	private long totalPages;
	private int nextPage;
	private long allsize;
 
	
	@Override
	public fileupload getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		int pageSize = 9;
		Page<fileupload> page = new Page<fileupload>();
		page.setAutoCount(true);
		page.setPageNo(currentpage);
		page.setPageSize(pageSize);
	    fileupload.getList(page);
		setFileList(page.getResult());
		pageNo = page.getPageNo();
		prePage = page.getPrePage();
		nextPage = page.getNextPage();
		totalPages = page.getTotalPages();
		if(totalPages==0){
			
			pageNo=0;
		}
		allsize = page.getTotalCount();
		return "list";
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		String root = ServletActionContext.getServletContext().getRealPath(
				"/upload");// 获取路径
//		long a= pictruepath.length();
//		System.out.println(a);
	if(pictruepathFileName!=""&&pictruepathFileName!=null){
		if (StringUtils.isEmpty(id)) {
               ;
		} else {
			entity = fileupload.getById(id);
			File imgFile = new File(root + "/" + entity.getPath());
			if (imgFile.exists()) {
				imgFile.delete();
			}
		}
		String fname = pictruepathFileName;                  // 重命名
//		if(entity.getName().equals(null)||entity.getName()==""){
			entity.setName(fname);
//		}
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyyMMddHHmmss");
		java.util.Date mydate = new java.util.Date();
		fname = new String(sdf.format(mydate)
				+ fname.substring(fname.indexOf(".")));
		entity.setPath(fname);                 //将新的文件名字戏写入entity中去 
		
		InputStream is = new FileInputStream(pictruepath);
		File destFile = new File(root, fname);
		OutputStream os = new FileOutputStream(destFile);
		byte[] buffer = new byte[1024];
		int length = 0;
		while (-1 != (length = is.read(buffer))) // 写入文件
		{
			os.write(buffer, 0, length);
		}
		is.close();
		os.close();
		
	}
	else{
		entity = fileupload.getById(id);
	}
	entity.setEditTime(DateUtils.now());
		fileupload.set(entity); // 保存
      Struts2Utils.renderText("r");
      return null;
	}
	public String show() {
		entity = fileupload.getById(id);
		return "addfile";
	}
	public String rename() {
		entity = fileupload.getById(id);
		entity.setName(name);
        fileupload.set(entity);
		return "save";
	}
	public String retime() {
		entity = fileupload.getById(id);
		entity.setEditTime(editTime);
        fileupload.set(entity);
		return "save";
	}
	@Override
	public String delete() throws Exception {
		entity = fileupload.getById(id);
		String root = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		File imgFile = new File(root + "/" + entity.getPath());
		if (imgFile.exists()) {
			imgFile.delete();
		}
		fileupload.delete(id);
			return "delete";
	}
	/**
	 * 按id批量删除
	 */
	public String deleteByIds() {
          for (String id : ids.split(";")) 
          {
	        entity = fileupload.getById(id);
	        String root = ServletActionContext.getServletContext().getRealPath(
					"/upload");
			File imgFile = new File(root + "/" + entity.getPath());
			if (imgFile.exists()) {
				imgFile.delete();
			}
	        fileupload.delete(id);
	       }
         return "delete";
	}
	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new fileupload();
		} else {
			entity = fileupload.getById(id);
		}
	}

	public fileupload getEntity() {
		return entity;
	}

	public void setEntity(fileupload entity) {
		this.entity = entity;
	}

	public File getPictruepath() {
		return pictruepath;
	}

	public void setPictruepath(File pictruepath) {
		this.pictruepath = pictruepath;
	}

	public String getPictruepathFileName() {
		return pictruepathFileName;
	}

	public void setPictruepathFileName(String pictruepathFileName) {
		this.pictruepathFileName = pictruepathFileName;
	}

	public String getPictruepathContenttype() {
		return pictruepathContenttype;
	}

	public void setPictruepathContenttype(String pictruepathContenttype) {
		this.pictruepathContenttype = pictruepathContenttype;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}


	public FileUploadManager getFileupload() {
		return fileupload;
	}
    @Autowired
	public void setFileupload(FileUploadManager fileupload) {
		this.fileupload = fileupload;
	}

	public List<fileupload> getFileList() {
		return fileList;
	}

	public void setFileList(List<fileupload> fileList) {
		this.fileList = fileList;
	}

	public int getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public String getEditTime() {
		return editTime;
	}

	public void setEditTime(String editTime) {
		this.editTime = editTime;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public long getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(long totalPages) {
		this.totalPages = totalPages;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public long getAllsize() {
		return allsize;
	}

	public void setAllsize(long allsize) {
		this.allsize = allsize;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


}
